/**  
* Copyright (c) 2008, Noel Lynch All rights reserved. Redistribution and use  
* in source and binary forms, with or without modification, are permitted  
* provided that the following conditions are met: Redistributions of source  
* code must retain the above copyright notice, this list of conditions and the  
* following disclaimer. Redistributions in binary form must reproduce the above  
* copyright notice, this list of conditions and the following disclaimer in the  
* documentation and/or other materials provided with the distribution. Neither  
* the name of Noel Lynch nor the names of contributors may be used to  
* endorse or promote products derived from this software without specific prior  
* written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
*/
// Jul 24, 2008

package com.sceneworker.editors.vertexprogrameditor;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Callable;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.jme.scene.state.VertexProgramState;
import com.jme.util.GameTaskQueueManager;
import com.myjavautils.swing.SwingComponentFactory;
import com.sceneworker.extensions.vertexprogramstate.VertexProgramStateCodePropertyObject;

public class VertexProgramStateCodeEditorPanel extends JPanel implements ActionListener {

	private		JButton										mcl_done;
	private		JButton										mcl_update;
	private		JDialog										mcl_par;
	private		JTextArea									mcl_text;
	private		VertexProgramStateCodePropertyObject		mcl_fragProgState;
	
	public
	VertexProgramStateCodeEditorPanel(JDialog cl_par, VertexProgramStateCodePropertyObject cl_programObj) {
		mcl_par = cl_par;
		
		mcl_fragProgState = cl_programObj;
		buildPanel(cl_programObj);
	}
	
	private	void
	buildPanel(final VertexProgramStateCodePropertyObject str_location) {
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		buildButtonPanel();
		
		mcl_text = new JTextArea();
		mcl_text.setText(mcl_fragProgState.getValue().getProgram());
		JScrollPane		lcl_scroller = new JScrollPane(mcl_text);
		lcl_scroller.setBounds(0, mcl_done.getY() + mcl_done.getHeight(), 400, 400);
		add(lcl_scroller);
		
	}
	
	private		void	buildButtonPanel() {
		JPanel		lcl_panel = new JPanel();
		lcl_panel.setLayout(new BoxLayout(lcl_panel, BoxLayout.LINE_AXIS));
		
		mcl_done = SwingComponentFactory.makeAButton("Done");
		mcl_done.addActionListener(new ActionListener() {
			public	void
			actionPerformed(ActionEvent cl_e) {
				mcl_par.setVisible(false);
			}
		});
		lcl_panel.add(mcl_done);
		lcl_panel.add(Box.createRigidArea(new Dimension(5, 0)));
		
		mcl_update = SwingComponentFactory.makeAButton("Update");
		mcl_update.addActionListener(this);
		lcl_panel.add(mcl_update);
		
		lcl_panel.setAlignmentX(Component.LEFT_ALIGNMENT);
		lcl_panel.add(Box.createHorizontalGlue());
		add(lcl_panel);
	}

	public void actionPerformed(ActionEvent cl_e) {
		if(cl_e.getSource() == mcl_update) {
			final	String		lstr_program = mcl_text.getText();
			final	VertexProgramState	lcl_fs = mcl_fragProgState.getValue();
			
			GameTaskQueueManager.getManager().update(new Callable<Object>() {

				public Object call() throws Exception {
					lcl_fs.load(lstr_program);
					return null;
				}
				
			});
		}
	}
	
}
